<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\model\api;

use app\model\api\genshin\GenshinLevelNumber;
use app\model\api\genshin\GenshinMaterials;
use laytp\BaseModel;
class GenshinRole extends BaseModel
{

    protected $deleteTime = 'delete_time';

    protected $skillArray = [1,2,3];
    /**
     * 角色列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRoleList(){
        $list = $this->field('id,role_name as name,element,star as stars,head_img as avatar,nation')
            ->cache(true)
            ->order('star desc')
            ->select()->toArray();
        return $list;
    }



    /**
     * 根据id获取对应角色信息
     * @return mixed
     */
    public function roleInfo(){
        $list = $this->cache(true)
            ->column('role_name,stand,head_img,element,star,nation','id');
        return $list;
    }

    /**
     *
     * @param $role_id
     * @return GenshinRole|array|mixed|null|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRoleInfo($role_id){
        return $this->where('id',$role_id)->find();
    }

    /**
     * 添加角色升级等级材料 （1-89）级
     * @param $role_id int 用户id
     * @return bool|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addRoleLevel($role_id){
        $count = $this->where('id',$role_id)->count('id');
        if($count==0){
            return false;
        }
        $role_level_model = new GenshinRoleLevel();
        $levels =  $role_level_model->where('role_id',$role_id)->column('level');
        if(count($levels)!=89){//等级经验不满足89条
            $level_number_list = (new GenshinLevelNumber())->where('type','role')->order('level')->select()->toArray();
            $materials = (new GenshinMaterials())->where('type','roleNormal')->order('id asc')->field('materials_id,name,icon')->select()->toArray();
            if(count($materials)!=2){
                return false;
            }
            $insert_data = [];
            foreach ($level_number_list as $item){
                if(!in_array($item['level'],$levels)){
                    $new_materials = [];
                    $new_materials[] =[
                      'id' =>$materials[0]['materials_id'],
                      'num'=>$item['number'],
                      'name'=>$materials[0]['name'],
                      'icon'=>$materials[0]['icon']
                    ];
                    $new_materials[] =[
                        'id' =>$materials[1]['materials_id'],
                        'num'=>$item['book_number'],
                        'name'=>$materials[1]['name'],
                        'icon'=>$materials[1]['icon']
                    ];
                    $insert_data[]=[
                      'role_id' => $role_id,
                      'level'=>$item['level'],
                      'material'=>json_encode($new_materials,256)
                    ];
                }
            }
            try {
                if(count($insert_data)>0){
                    $role_level_model->saveAll($insert_data);
                }
                return true;
            } catch (\Exception $e) {
                return false;
            }
        }
        return true;
    }

    /**
     * 添加角色升级等级材料 （1-89）级
     * @param $role_id int 用户id
     * @return bool|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addSkillLevel($role_id){
        $count = $this->where('id',$role_id)->count('id');
        if($count==0){
            return false;
        }
        $skill_level_model = new GenshinSkillLevel();
        $levels =  $skill_level_model->where('role_id',$role_id)->field('level,skill_id')->select()->toArray();
        if(count($levels)!=27){//技能等级不满足27条 （3个技能每个 9级）
            $level_number_list = (new GenshinLevelNumber())->where('type','skill')->order('level')->select()->toArray();
            $materials = (new GenshinMaterials())->where('type','skillNormal')->order('id asc')->field('materials_id,icon,name')->select()->toArray();
            if(count($materials)!=1){
                return false;
            }
            $levels = array_map(function ($item){return $item['skill_id'].$item['level'];},$levels);
            $insert_data = [];

            foreach ($this->skillArray as $item){
                foreach ($level_number_list as $i){
                    if(!in_array($item.$i['level'],$levels)){
                        $new_materials = [];
                        $new_materials[] =[
                            'id' =>$materials[0]['materials_id'],
                            'name' =>$materials[0]['name'],
                            'num'=>$i['number'],
                            'icon'=>$materials[0]['icon']
                        ];
                        $insert_data[]=[
                            'role_id' => $role_id,
                            'skill_id' => $item,
                            'level'=>$i['level'],
                            'material'=>json_encode(['avatar_skill_consume'=>$new_materials],256)
                        ];
                    }
                }
            }
            try {
                if(count($insert_data)>0){
                    $skill_level_model->saveAll($insert_data);
                }
                return true;
            } catch (\Exception $e) {
                return false;
            }
        }
        return true;
    }

}